package cn.vshare.util;


import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class VideoUtil {
    public static Map<String,Object> currTimeMap=new HashMap<>();
    private String totalTime="1";
    private String currTime="1";
    private String finish="no";
    public static String[] allowFiles = {".mp4", ".flv", ".avi", ".mpg", ".wmv", ".3gp", ".mov",
            ".asf", ".asx", ".vob"};
    public static String[] allowFLV = {".avi", ".mpg", ".wmv", ".3gp", ".mov", ".asf", ".asx", ".vob"};
    //  public static String[] allowAVI = {".wmv9", ".rm", ".rmvb"};
    public static String ffmpegPath = "E:\\javaworkspace\\VShare\\src\\main\\webapp\\tool\\ffmpeg.exe";
    // public static String mencoderPath = "E:\\javaworkspace\\VShare\\src\\main\\webapp\\tool\\mencoder.exe";

    /**
     * 所有的参数均为电脑的本地路径
     *
     * @param ffmpegPath
     * @param begingFilePath
     * @param endFilePath
     */
    public  void processFlv(String videoUtilId,String ffmpegPath, String begingFilePath, String endFilePath) throws IOException {
        List<String> convert = new ArrayList<>();
        convert.add(ffmpegPath); // 添加转换工具路径
        convert.add("-i"); // 添加参数＂-i＂，该参数指定要转换的文件
        convert.add(begingFilePath); // 添加要转换格式的视频文件的路径
        convert.add("-ab");
        convert.add("56");
        convert.add("-c:v");
        convert.add("libx264");
        convert.add("-ar");
        convert.add("22050");
        convert.add("-q:a");
        convert.add("5");
        convert.add("-r");
        convert.add("15");
        convert.add("-s");
        convert.add("1980*1020");
        convert.add(endFilePath);
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(convert);
        Process process=null;
        try {
            process=processBuilder.start();
            MyThread myThread=new MyThread(process,new VideoUtil(),begingFilePath,videoUtilId);
            myThread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
      /*  // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
            if (br != null) {
                br.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
            if (errorStream != null) {
                errorStream.close();
            }
        }
*/
    }


    /**
     * 所有的参数均为电脑的本地路径
     *
     * @param ffmpegPath
     * @param begingFilePath
     * @param endPicPath
     */
    public  void getPic(String ffmpegPath, String begingFilePath, String endPicPath) {
        List<String> command = new ArrayList<>();
        command.add(ffmpegPath);
        command.add("-y");
        command.add("-i");
        command.add(begingFilePath);
        command.add("-vframes");
        command.add("1");
        command.add("-ss");
        command.add("0:0:2");
        command.add("-f");
        command.add("mjpeg");
        command.add("-s");
        command.add("1920*1080");
        command.add("-an");
        command.add(endPicPath);
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(command);
        try {
            Process process = processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  boolean checkAllowFiles(String fileName) {
        for (String end : allowFiles
                ) {
            if (fileName.toLowerCase().endsWith(end)) {
                return true;
            }
        }
        return false;
    }

/*    public static boolean checkAvi(String fileName){
       for (String end :allowAVI){
           if (fileName.toLowerCase().endsWith(end)){
               return true;
           }
       }
       return false;
    }*/

    public  boolean checkFlv(String fileName) {
        for (String end : allowFLV
                ) {
            if (fileName.toLowerCase().endsWith(end)) {
                return true;
            }
        }
        return false;
    }


    public String getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(String totalTime) {
        this.totalTime = totalTime;
    }

    public String getCurrTime() {
        return currTime;
    }

    public void setCurrTime(String currTime) {
        this.currTime = currTime;
    }

    public String getFinish() {
        return finish;
    }

    public void setFinish(String finish) {
        this.finish = finish;
    }
}
